package com.yao.spider.core.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created by 单耀 on 2017/12/11.
 * 线程池工具类，建始县城池执行情况
 */
public class ThreadPoolMonitor implements Runnable{
    private static Logger logger = LoggerFactory.getLogger(ThreadPoolMonitor.class);
    private ThreadPoolExecutor executor;
    private String name = "";
    public static volatile boolean isMonitor = false;
    public ThreadPoolMonitor(ThreadPoolExecutor executor, String name) {
        this.executor = executor;
        this.name = name;
    }
    public void run() {
        while(!isMonitor) {
            logger.debug(name +
                String.format("[monitor] [%d/%d] Active: %d, Completed: %d, queueSize: %d, Task: %d, isShutdown: %s, isTerminated: %s",
                        this.executor.getPoolSize(),
                        this.executor.getCorePoolSize(),
                        this.executor.getActiveCount(),
                        this.executor.getCompletedTaskCount(),
                        this.executor.getQueue().size(),
                        this.executor.getTaskCount(),
                        this.executor.isShutdown(),
                        this.executor.isTerminated()));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
